home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
BSSTACK.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-08-04
|
6KB
|
207 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: bsstack.c
// Title: Base library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains a debug stack monitor.
// It contains routines to monitor stack space usage under DOS and
// Borland C++ only.
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <bs.h>
//----------------------------------------------------------------------------
// Stack probe
//----------------------------------------------------------------------------
#if COMPILER_BORLAND
extern unsigned _stklen;
#endif
#define SIZE_OF_EMULATOR (415) // Account for floating
#define STACK_FILL_CHAR (0x5A) // Character used to mark the stack
#if OS_WINDOWS // Windows local heap information
typedef struct
{
BYTE bReserved[6];
BYTE _NEAR_ *npbLocalHeap;
BYTE _NEAR_ *npbAtomTable;
BYTE _NEAR_ *npbStackTop;
BYTE _NEAR_ *npbStackMin;
BYTE _NEAR_ *npbStackBtm;
} LOCALHEAPINFO;
BASETYPE(LOCALHEAPINFO);
#endif
//----------------------------------------------------------------------------
// Description: Check stack high-water mark. Scan the stack until a changed
// word is found.
// This routine only functions under DOS and Borland C++ v3.1
// using the large memory model.
// It assumes that the stack is located in a separate segment.
// Parameters:
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
VOID FN_E CheckStack(void)
{
#if COMPILE_DEBUG
FILE *file;
unsigned count= 0; // Amount of unused stack space
unsigned max = 0;
#if COMPILER_BORLAND && OS_DOS
char far *sp; // Far memory model pointer
// Initialize the pointer
sp = MK_FP(_SS, SIZE_OF_EMULATOR+1);
if(_SP > SIZE_OF_EMULATOR) // Check for stack overflow
{
while(sp < MK_FP(_SS,_SP)) // Check for stack overflow
{
if(*sp != (char)STACK_FILL_CHAR) // Compare pointer value to
break; // fill character
count++; // Count unused stack space
sp++; // Increment pointer
}
}
Assert(count <= _stklen);
count = (SIZET)(_stklen - count);
max = _stklen;
#elif OS_WINDOWS
PLOCALHEAPINFO plhi;
# if !COMPILER_BORLAND
unsigned _SS;
_asm {
mov ax, ss
mov [_SS], ax
mov ax, 01h
}
# endif
plhi = MAKEP(_SS, 0);
if (plhi->npbStackTop)
{
PBYTE pbStackTop = MAKEP(_SS, plhi->npbStackTop);
PBYTE pbStackBtm = MAKEP(_SS, plhi->npbStackBtm);
pbStackTop++;
max = (SIZET)(pbStackBtm - pbStackTop);
while (*pbStackTop == STACK_FILL_CHAR)
pbStackTop++;
count = (SIZET)(pbStackBtm - pbStackTop);
}
#endif
if (!LogEnabled())
return ;
if (max && (file = fopen("stack.log", "a+t")) != NULL)
{
fprintf(file, "%u of %u stack bytes used (%ld %%).\n",
count, (SIZET)max, ((LONG)count * 100L) / (LONG)max);
fclose(file);
}
#endif
return ;
}
//----------------------------------------------------------------------------
// Description: This routine clear the stack to a predetermined value.
// These values will be checked at termination to determine
// a high water mark on the stack.
// This routine only functions under DOS and Borland C++ v3.1
// using the large memory model and under Windows
// Under DOS, it assumes that the stack is located in a
// separate segment.
// Parameters:
// Returns:
//----------------------------------------------------------------------------
VOID FN_E ClearStack(void)
{
#if COMPILE_DEBUG
#if COMPILER_BORLAND && OS_DOS
char far *sp; // Far memory model stack pointer
sp = MK_FP(_SS, _SP - 1); // Initialize sp to point to
// the next available space on
// the stack.
LogEnabled(); // Call to be sure globals initialized
while(sp > (char far *)SIZE_OF_EMULATOR)
{
*sp = STACK_FILL_CHAR; // Initialize unused stack space
sp--;
}
BaseExitFunc((PFNEXIT)CheckStack, SYS_EXIT_PRIORITY+1);
#elif OS_WINDOWS
PLOCALHEAPINFO plhi;
# if !COMPILER_BORLAND
unsigned _SP, _SS;
_asm {
mov ax, sp
mov [_SP], ax
mov ax, ss
mov [_SS], ax
}
# endif
plhi = MAKEP(_SS, 0);
LogEnabled(); /* Call to be sure globals initialized */
if (plhi->npbStackTop)
{
PBYTE pbStackTop = MAKEP(_SS, plhi->npbStackTop);
PBYTE pbStackNow = MAKEP(_SS, _SP);
pbStackNow -= 2;
while (pbStackTop < pbStackNow)
*pbStackNow-- = STACK_FILL_CHAR;
}
BaseExitFunc((PFNEXIT)CheckStack, SYS_EXIT_PRIORITY+1);
#endif
#endif
return;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------